<?php
namespace Api\Controller;
use Think\Controller;

class AjaxController extends Controller{
	function get_tuijain_js()
	{
		$number=M('user')->where(array('username'=>I('tuijian')))->find();
		if($number)
		{
			$return['name']=$number['realname'];

		}else{
			$return['name']='此人不存在';
		}
		$return['status']=1;
		$this->ajaxReturn($return);
	}
	public function  get_user_balance()
	{
		$uid=I("uid");
		$money_type=I("money_type");
		$info=M("user")->find($uid);
		$return['status']=1;
		$return['balance']=$info['balance_'.$money_type];
		$return['name']=get_money_name_byident($money_type);
		$this->ajaxReturn($return);
	}
	public function orgChartGroup(){
		$active_userid=I('active_userid');//被激活人id
		$active_userinfo=M('User')->where(array('id'=>$active_userid))->find();//被激活人信息
		$active_tuijian_userinfo=M('User')->where(array('id'=>$active_userinfo['refereeid']))->find();//被激活人的推荐人信息
		$fist_groupinfo=M('Group')->where(array('id'=>$active_tuijian_userinfo['groupid']))->find();//初始组信息
		
		//搜索组信息
		$key_group_name=I('keyword');
		if(!empty($key_group_name)){
			$groupinfo=$key_groupinfo=M('Group')->where(array('group_name'=>$key_group_name))->find();//搜索组信息
		}
		//点击组信息
		$click_group_id=I('gid');
		if(!empty($click_group_id)){
			$groupinfo=$click_groupinfo=M('Group')->where(array('id'=>$click_group_id))->find();//点击组信息
		}
		
		if(empty($groupinfo)){
			$groupinfo=$fist_groupinfo;
		}else{
			//判断搜索点击的用户在不在初始用户伞下
			$group_parent_info=M('GroupParent')->where(array('gid'=>$groupinfo['id'],'parentid'=>$fist_groupinfo['id']))->find();
			if(empty($group_parent_info)){
				$groupinfo=$fist_groupinfo;
			}
		}
		
		$groupinfo['gid']=$groupinfo['id'];
		$zhixiao_type=C('ZHIXIAO_TYPE');
		if(empty($zhixiao_type)){
			$this->error('太阳线模式无接点图');
		}
	
		$groupson1=orgDataGroup($groupinfo['gid']);
		foreach ($groupson1 as $k=>$v){
			$groupson2=orgDataGroup($v['gid']);
			$groupson1[$k]['_child']=$groupson2;
		}
		$groupinfo['_child']=$groupson1;
		$this -> assign('groupinfo', $groupinfo);
		$this -> assign('active_userid', $active_userid);
		$this -> assign('fist_groupinfo', $fist_groupinfo);
		$this->search = array('title' => '请输入组编号','url'=>U('Ajax/orgChartGroup',array('active_userid'=>$active_userid)));
		$this->display();
	}
	
	
	/**
	 * 获取创业组推荐树数据
	 * fty170
	 */
	public function get_group_tree(){
		$list=array();
		$initial_gid=I('initial_gid');//登录者组id
		$id=I('id');
		if($id>0){
			$son_gids=M('GroupReferee')->where(array('refereeid'=>$id,'distance'=>1))->getField('gid',true);
		}else{
			$son_gids=$initial_gid;
		}
		$son_list=M("Group")->where(array('id'=>array('in',$son_gids)))->select();
		foreach ($son_list as $k=>$v){
			$next_count=M("Group")->where(array('tid'=>$v['id']))->count();
			if($next_count>0){
				$son_list[$k]['isParent']= true;
			}else{
				$son_list[$k]['isParent']= false;
			}
			$son_list[$k]['user_name1']=get_username($v['userid1']);
			$son_list[$k]['user_name2']=get_username($v['userid2']);
			$son_list[$k]['user_name3']=get_username($v['userid3']);
		}
		$this->ajaxReturn($son_list);
	}
	
	
	/**
	 * 获取用户密保问题
	 */
	public function get_user_question(){
		$return['status']=true;//状态
		$return['msg']="";//信息
		$return['data']="";//数据
		$username=I('username');
		$userinfo=M('User')->where(array('user_name'=>$username))->find();
		if(empty($userinfo)){
			$return['status']=false;//状态
			$return['msg']="用户不存在或被禁用";//信息
			$this->ajaxReturn($return);
		}
		$num=mt_rand(1, 3);
		$question_num="question".$num;
		$question=$userinfo[$question_num];
		if(empty($question)){
			$return['status']=false;//状态
			$return['msg']="未完善密保,请换其他方式找回密码";//信息
			$this->ajaxReturn($return);
		}
		$data['qnum']=$num;
		$data['question']=$question;
		$return['data']=$data;//数据
		$this->ajaxReturn($return);
	}
	/**
	 * 登录主题效果详情图片
	 */
	public function show_login_theme_info(){
		/* {
		 "title": "", //相册标题
		"id": 123, //相册id
		"start": 0, //初始显示的图片序号，默认0
		"data": [   //相册包含的图片，数组格式
		{
		"alt": "图片名",
		"pid": 666, //图片id
		"src": "", //原图地址
		"thumb": "" //缩略图地址
		}
		]
		} */
		$id=I('id');
		$info=M('LoginTheme')->where(array('id'=>$id))->find();
		$effect=$info['effect'];
		$effect_arr=config_str_to_arr($effect);
		foreach ($effect_arr as $ek=>$ev){
			$data_effect['alt']=$ev;
			$data_effect['pid']=$ek;
			$data_effect['src']="./Public/logins/".$info['name']."/img/xg_".$ek.".png";
			$data_effect['thumb']="./Public/logins/".$info['name']."/img/xg_".$ek.".png";
			$data_arr[]=$data_effect;
		}
		if(empty($data_arr)){
			$return['status']=false;
		}else{
			$return['status']=true;
			$return['title']="效果详情";
			$return['id']=$info['id'];
			$return['statrt']=0;
			$return['data']=$data_arr;
		}
		$this->ajaxReturn($return);
	}
	//验证超级密码
	public function check_admin_password_js(){
		$return['status']=true;//状态
		$return['msg']="";//信息
		$return['data']="";//数据
		$admin_password=C('ADMIN_PASSWORD');
		$admin_pass=I('admin_pass');
		if(empty($admin_pass)){
			$return['status']=false;//状态
			$return['msg']="请输入超级密码";//信息
			$this->ajaxReturn($return);
		}
		if($admin_password!=$admin_pass){
			$return['status']=false;//状态
			$return['msg']="超级密码错误";//信息
			$this->ajaxReturn($return);
		}else{
			$return['msg']="验证成功";//信息
			$this->ajaxReturn($return);
		}
	
	}
	//验证用户编号
	public function check_usernumber_js(){
		$return['status']=true;//状态
		$return['msg']="";//信息
		$return['data']="";//数据
		$usernumber=I('usernumber');
		$userinfo=M('User')->where(array('username'=>$usernumber))->find();

		if(!empty($userinfo)){
			$return['status']=false;//状态
			$return['msg']="该用户编号己存在！";//信息
		}else{
			$return['msg']="该用户编号可以使用！";//信息
		}
		$this->ajaxReturn($return);
	}
	//通用接点关系
	public function get_parentlist_js(){
		//直销模式
		$zhixiao_type=C('ZHIXIAO_TYPE');
		$parentid=I('parentid');
		$map['status']=1;
		$map['user_level']=array('neq',100);//不是管理员
		$map['parentid']=array('neq',0);//已排网
		$map['id']=array('gt',1);
		$list=M('User')->where($map)->select();
		$data = "<option value =''>-请选择-</option>";
		
		foreach ($list as $k => $vo) {
			
			$jiedian_count=M('UserParent')->where(array('parentid'=>$vo['id'],'distance'=>1))->count();
			if($jiedian_count<$zhixiao_type){
				$data .= "<option ";
				if($vo['id']==$parentid){
					$data .= " selected='selected' ";
				}
				$data .= " value ='" . $vo['id'] . "'>" . $vo['user_name']. "</option>";
			}
		}
		$this->ajaxReturn($data);
	}
	//获取报单中心
	public function get_user_server_js(){
		$return['status']=true;//状态
		$return['msg']="";//信息
		$return['data']="";//数据
		$tuijiannumber=I('tuijiannumber');
		$tuijianinfo=M('User')->where(array('user_name'=>$tuijiannumber,'status'=>1))->find();
		if(empty($tuijianinfo)){
			$return['status']=false;//状态
			$return['msg']="该推荐人不存在";//信息
			$this->ajaxReturn($return);
		}
		if($tuijianinfo['is_server_center']==1){
			$return['data']=$tuijianinfo;//数据
			$this->ajaxReturn($return);
		}else{
			$tuijianids=M('UserReferee')->where(array('userid'=>$tuijianinfo['id']))->Order('distance ASC')->getField('refereeid',true);
			foreach ($tuijianids as $v){
				$tuijianinfo=M('User')->where(array('id'=>$v,'status'=>1))->find();
				if($tuijianinfo['is_server_center']==1){
					$return['data']=$tuijianinfo;//数据
					$this->ajaxReturn($return);
					break;
				}
			}
				
		}
	}
	public function get_pos_js(){
		$parentid=I('parentid');
		$pos=I('pos');
		$poss=M('UserParent')->where(array('parentid'=>$parentid,'distance'=>1))->getField('position',true);
		//获取分区数组
		$list=get_pos_arr();
		$data = "<option value =''>-请选择-</option>";
		if(!empty($parentid)){
			foreach ($list as $k => $vo) {
				$data .= "<option ";
				if(in_array($vo['id'], $poss) ){
					$data .= " disabled='true' ";
					$vo['name']=$vo['name']."【已注册】";
				}
				if($vo['id']==$pos){
					$data .= " selected='selected' ";
				}
				$data .= " value ='" . $vo['id'] . "'>" . $vo['name'] . "</option>";
			}
		}
		$this->ajaxReturn($data);
	}
	//通用推荐关系
	public function get_refereelist_js(){
		$refereeid=I('refereeid');
		
		$map['status']=1;
		$map['user_level']=array('neq',99);//不是管理员
		$map['refereeid']=array('neq',0);//已排网
		$list=M('User')->where($map)->select();
		$data = "<option value =''>-请选择-</option>";
		foreach ($list as $k => $vo) {
			$data .= "<option ";
			if($vo['id']==$refereeid){
				$data .= " selected='selected' ";
			}
			$data .= " value ='" . $vo['id'] . "'>" . $vo['user_name']. "</option>";
		}
		$this->ajaxReturn($data);
	}
	//用户列表
	public function get_userlist_sel_js(){
	
		$user_keyword = I('user_keyword');
		if(!empty($user_keyword)){
			$condition = array('like','%'.$user_keyword.'%');
			$map['id|user_name|email|mobile'] = array($condition, $condition, $condition, $condition,'_multi'=>true);
		}else{
			$user_map_json=I('user_map_json');
			$map=$user_map_json;
		}
	
	
		$map['status']=1;
		$map['user_level']=array('lt',99);//不是管理员
		$list=M('User')->where($map)->select();
		foreach ($list as $k => $vo) {
			$data .= "<option value ='" . $vo['id'] . "'>" . $vo['user_name'].'['.$vo['true_name'].']'. "</option>";
		}
		$this->ajaxReturn($data);
	}
	/**
	 * 币种转换begin
	 */
	public function get_frommoney_js(){
		//币种
		$money_type_list=M('MoneyType')->where(array('status'=>1))->select();
		$data = "<option value =''>-请选择-</option>";
		foreach ($money_type_list as $k => $vo) {
			if($vo['transfer_money']!=''){
				$data .= "<option value ='" . $vo['identification'] . "'>" . $vo['name'] . "</option>";
			}
			
		}
		$this->ajaxReturn($data);
	}
	public function get_tomoney_js(){
		$frommoneytype=I('frommoneytype');
		$data = "<option value =''>-请选择-</option>";
		if(empty($frommoneytype)){
			$this->ajaxReturn($data);
		}
		$frommoneytype_info=M('MoneyType')->where(array('identification'=>$frommoneytype))->find();
		$transfer_money=$frommoneytype_info['transfer_money'];
		
		$money_type_list=M('MoneyType')->where(array('status'=>1,'identification'=>array('in',$transfer_money)))->select();
		
		foreach ($money_type_list as $k => $vo) {
			$data .= "<option value ='" . $vo['identification'] . "'>" . $vo['name'] . "</option>";
		}
		$this->ajaxReturn($data);
	}
	/**
	 * 币种转换end
	 */
	
	/**
	 * 显示充值备注详情
	 */
	public function show_recharge_bz(){
		$id=I('id');
		$info=M('Recharge')->where(array('id'=>$id))->find();
		$data.="用户备注:".$info['remark_user']."<br/>";
		$data.="公司备注:".$info['remark_admin']."<br/>";
		print_r($data);
		exit;
	}
	/**
	 * 显示提现详情
	 */
	public function show_with_info(){
		$id=I('id');
		$with_info=M('Withdrawals')->where(array('id'=>$id))->find();
		$data.="开户名:".$with_info['name']."<br/>";
		$data.="开户行:".$with_info['bank']."<br/>";
		$data.="银行账号:".$with_info['bank_no']."<br/>";
		print_r($data);
		exit;
	}
	public function show_with_info_refuse(){
		$id=I('id');
		$with_info=M('Withdrawals')->where(array('id'=>$id))->find();
		if($with_info['process_status'] == -1)
		{
			$data.="拒绝审核原由:".$with_info['remark']."<br/>";
		}elseif($with_info['process_status'] == 2){
			$data.="审核通过<br/>";
		}
		print_r($data);
		exit;
	}
	/**
	 * 显示流水详情
	 */
	public function show_bonus_change_info(){
		$id=I('id');
		$money_type=I('group');
		$moneychange_model=D('Detailed'.ucfirst($money_type));
		$info=$moneychange_model->where(array('id'=>$id))->find();
		$data.="备注:".$info['remark']."<br/>";
		print_r($data);
		exit;
	}
	/**
	 * 显示日志详情
	 */
	public function show_log_info(){
		$id=I('id');
		$info=M("Log")->where(array('id'=>$id))->find();
		$data.="详情:".$info['content']."<br/>";
		print_r($data);
		exit;
	}
	/**
	 * 奖金设置
	 */
	public function bonus_set(){
		$return['status']=true;//状态
		$return['msg']="";//信息
		$return['data']="";//数据
		$id=I('id');
		$val=I('val');
		$uname=$_SESSION['CT_Admin_']['user_auth']['user_name'];
		$data_arr=explode('-',$id);
		//字段
		$data_zd=$data_arr['0'];
		//id
		$data_id=$data_arr['1'];
		//模型
		$data_mod=$data_arr['2'];
		if(empty($data_mod)){
			$data_mod="BonusConfig";
		}
// 		if($val<0 || !(is_numeric($val)))
// 		{
// 			$return['status']=false;//状态
// 			$return['msg']='提交数据不正确';//信息
// 			$this->ajaxReturn($return);
// 		}
		$data['id']=$data_id;
		$data[$data_zd]=$val;
		$info=M($data_mod)->find($data_id);

		$res=M($data_mod)->save($data);
		if($res){
			$return['msg']='操作成功';//信息

			$log_data['title']="修改直销制度";
			$log_data['content']="用户".$uname."修改".$info['name']."。原始值：".$info[$data_zd].",修改为:".$val;
			$log_data['ext']="用户".$uname."修改".$info['name']."。之前值：".$info[$data_zd].",修改为:".$val.".备注：库,".$data_mod.'。id,'.$data_id.'。字段,'.$data_zd;
				
			add_log_message($log_data,""); 
			$this->ajaxReturn($return);
		}else{
			$return['status']=false;//状态
			$return['msg']='操作失败';//信息
			$this->ajaxReturn($return);
		}
	}
	/**
	 * 获取地区列表
	 */
	public function get_areas_list(){
		$m = M('Areas');
		$parentId=I('parentId');
		$list = $m->where('areaFlag=1 and parentId='.(int)$parentId)->select();
		$rs = array();
		$rs['status'] = 1;
		$rs['list'] = $list;
		$this->ajaxReturn($rs);
	}
	/**
	 * 获取店铺分类列表
	 */
	public function get_shops_cats_list(){
		$m = M('shops_cats');
		$parentId=I('parentId');
		$list = $m->where('catFlag=1 and parentId='.(int)$parentId)->select();
		$rs = array();
		$rs['status'] = 1;
		$rs['list'] = $list;
		$this->ajaxReturn($rs);
	}
	/**
	 * 商品分类列表
	 */
	public function get_goods_cats_list(){
		$m = M('goods_cats');
		$parentId=I('pid');
		$list = $m->where('catFlag=1 and pid='.(int)$parentId)->select();
		$rs = array();
		$rs['status'] = 1;
		$rs['list'] = $list;
		$this->ajaxReturn($rs);
	}
}